import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    showLoginModal: false,
    user: null
  },
  mutations: {
    setShowLoginModal(state, show) {
      state.showLoginModal = show
    },
    setUser(state, user) {
      state.user = user
    }
  },
  actions: {
    async login({ commit }, credentials) {
      try {
        // 调用登录API
        const response = await this.$api.auth.login(credentials)
        commit('setUser', response.data)
        return response
      } catch (error) {
        throw error
      }
    }
  }
})